/*这段代码是固定的，必须要放到js中*/
function android_setupWebViewJavascriptBridge(callback) {
    //android使用
    if (window.WebViewJavascriptBridge) {
        callback(window.WebViewJavascriptBridge)
    } else {
        document.addEventListener(
            'WebViewJavascriptBridgeReady'
            , function() {
                callback(window.WebViewJavascriptBridge)
            },
            false
        );
    }
   

}
function ios_setupWebViewJavascriptBridge(callback) {

    //ios使用
    if (window.WebViewJavascriptBridge) {
        return callback(WebViewJavascriptBridge);
    }
    if (window.WVJBCallbacks) {
        return window.WVJBCallbacks.push(callback);
    }
    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function () {
        document.documentElement.removeChild(WVJBIframe)
    }, 0)
}


function android_jsAsyncCallHandler(name, param) {
    return new Promise((reslove) => {
        android_setupWebViewJavascriptBridge(function (bridge) {
            
            if (param) {
                // 第二个参数为 函数时；
                bridge.callHandler(name, param, function (response) {
                    reslove(response);
                });
            } else {
                bridge.callHandler(name, null, function (response) {
                    reslove(response);
                });
            }
        });
    });
}
function ios_jsAsyncCallHandler(name, param) {
    return new Promise((reslove) => {
        ios_setupWebViewJavascriptBridge(function (bridge) {
         
            if (param) {
                // 第二个参数为 函数时；
                bridge.callHandler(name, param, function (response) {
                    reslove(response);
                });
            } else {
                bridge.callHandler(name, function (response) {
                    reslove(response);
                });
            }
        });
    });
}
if(navigator.userAgent.indexOf('iPhone')<0){
    android_setupWebViewJavascriptBridge(function (bridge) {
        bridge.init(function(message, responseCallback) {
                    
        });
    })
}
window.jsBridge = {
    getUserId: function () {
        if(navigator.userAgent.indexOf('iPhone')>0){
            return ios_jsAsyncCallHandler('getWatchOpenInfo');
        }else{
            return android_jsAsyncCallHandler('getWatchOpenInfo');
        }
        
    },
    setBar: function (payload) {
        document.title = payload.title;
        var params = {
            hidden: 0, // 1不显示标题文字（可以设置成空字符），0显示标题文字
            backstyle: 'white', // 返回按钮类型，white白，black黑
            bgcolor: payload.bgColor, // 导航栏背景颜色
            text: payload.title, // 文字
            color: payload.txtColor, // 文字颜色
            fullscreen: 0, // 全屏模式，不显示导航栏。1全屏，0不全屏
        };
        if(navigator.userAgent.indexOf('iPhone')>0){
            return ios_jsAsyncCallHandler('setPageBar', params);
        }else{
            return android_jsAsyncCallHandler('setPageBar', params);
        }
    },
};
